import {Component, Inject, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {recodeModule} from '../../../service/delivent/recodeModule';
import {UtilityService} from '../../../service/utils.service';
import {appConfig} from '../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {DA_SERVICE_TOKEN, ITokenService} from '@delon/auth';
import * as moment from 'moment';

@Component({
    selector: 'app-revert-list',
    templateUrl: './revert-list.component.html',
      styleUrls: ['./revert-list.component.less'],
})
export class RevertListComponent implements OnInit {
 constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
         private confirmServ: NzModalService,
       @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    ) { }
        role:any;
     ngOnInit() {
        this.role = this.tokenService.get().role;
        this.getData();
     

    }
    data:any[]=[]
    listloading = false;
    showAdd = true;
    isPagination = true;
    expandForm  = false;
    loading = false;
    headerDate = [  // 配置表头内容

        { label: '工作项', value: 'itemName', isclick: false, radio: false },
        { label: '提交申请时间', value: 'rollbackTime', isclick: false, radio: false },
        { label: '申请人', value: 'applyPerson', isclick: false, radio: false },
        { label: '回退结果', value: 'rollbackStatus', isclick: false, radio: false },
          { label: '回退理由', value: 'rollbackDesc', isclick: false, radio: false },
    ];
 pageIndex = 1;

 search ={
     rollbackStatus:null,
     itemName:null
 }
 rollbackStatus = [
     {label:'已回退',value:'0'},
     {label:'回退中',value:'1'},
     
 ]
 total:number;
 pageTotal:number;
 getData(option?) {
        if (option) {
           this.pageIndex =1;
        }
        
          
        let page = {

                condition: this.search, // 搜索内容
                page: {
                    current: this.pageIndex,
                    size: 10,
                    orderByField: 'guid',
                    asc: false // asc 默认是true  升序排序，时间类型 用false， 降序
                }
            };
       this.listloading = true;
        this.utilityService.postData(appConfig.testUrl  + '/sRollback/list', page )
            .subscribe(
                (val) => {
                     this.data = val.result.records;
                        this.total = val.result.total; // 总数
                        this.pageTotal = val.result.pages * 10; // 页码
                          for ( let i = 0; i < this.data.length; i++) {
                            this.data[i].rollbackTime = moment(this.data[i].rollbackTime).format('YYYY-MM-DD');
                             if(this.role==="RCT小组"){
                            if(this.data[i].rollbackStatus !== "已回退"){
                                                            
                                           this.data[i].buttonData= [
                                                               {key: 'dels', value: '删除' },
                                                                 {key: 'detail', value: '详细'},
                                                                {key: 'rollback', value: '确认回退'}
                                                       ]
                                             }else{
                                                                    this.data[i].buttonData= [                                       
                                                                    {key: 'detail', value: '详细'},                                    
                                                                ]
                                                            }
                             }else{
                                         if(this.data[i].rollbackStatus !== "已回退"){
                                                            
                                           this.data[i].buttonData= [
                                                               {key: 'dels', value: '删除' },
                                                                 {key: 'detail', value: '详细'},
                                                                
                                                       ]
                                                    }else{
                                                                    this.data[i].buttonData= [                                       
                                                                    {key: 'detail', value: '详细'},                                    
                                                                ]
                                                            }
                             }
                           
                        }
                        this.listloading = false;
                
                },(error)=>{
                        this.listloading = false;
                this.nznot.create('error', error.code , error.msg);
                })
    }

   
    monitorHandler(it){

    }

     deleatData(it){

    }
    checkModalData = []
  revertListModal = false;
  id:number;
    buttonEvent(it){
        this.id = it.guid;
         let self = this;
        switch(it.names.key){
            case 'dels':
           
            this.confirmServ.confirm({
                title  : '提示',
                content:'<b>您是否确认要删除此项回退申请!</b>',
                showConfirmLoading: true,
                onOk() {
                    self.utilityService.deleatData(appConfig.testUrl  + '/sRollback/' + it.guid)
                        .subscribe(
                            (val) => {
                                if (val.code === '200') {
                                    if ( !(( self.total - 1) % 10)) {
                                        self.pageTotal = self.pageTotal - 10 ;
                                    }
                                    self.getData();

                                    self.nznot.create('success', val.msg, val.msg);
                                }else {
                                    self.nznot.create('error', val.msg, '');
                                }
                            }   ,
                            (error) => {
                                self.nznot.create('error', error.msg, '');

                            }
                        );
                },
                onCancel() {
                }
            });
            break;
            case 'detail':
     this.listloading = true;
            this.utilityService.getData(appConfig.testUrl  + '/sWorkitem/rollbackListByGuid/'+this.id)
            .subscribe(
                (val) => {
                    
                     this.checkModalData = val.result;
                  this.listloading = false;
                  this.revertListModal = true;
                },(error)=>{
                  this.listloading = false;
                  this.nznot.create('error', error.code , error.msg);
                })
          break;
          case 'rollback':
          
            this.confirmServ.confirm({
                title  : '提示',
                content:'<b>您是否确认要确认回退申请!</b>',
                showConfirmLoading: true,
                onOk() {
                    self.utilityService.getData(appConfig.testUrl  + '/sRollback/'+it.guid+'/affirmRollback')
                        .subscribe(
                            (val) => {
                                if (val.code === '200') {
                                
                                    self.getData();

                                    self.nznot.create('success', val.msg, val.msg);
                                }else {
                                    self.nznot.create('error', val.code,val.msg);
                                }
                            }   ,
                            (error) => {
                                self.nznot.create('error', error.code,error.msg);

                            }
                        );
                },
                onCancel() {
                }
            });
            break;

        }
         
    }

reset(){
    this.search ={
     rollbackStatus:null,
     itemName:null
 }
 this.getData();
}
  

}